package com.sky.dao;

import com.sky.bean.AdminBean;
import com.sky.bean.AdminBean;
import com.sky.util.DBConnection;
import com.sky.util.Time;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDao {
    DBConnection DBConn = new DBConnection();
////
    public boolean CheckLogin(String s1, String s2) throws SQLException {
        Connection conn = DBConn.getConn();
        Statement stmt = conn.createStatement();
        boolean OK = true;//true登录成功 false登录失败
        String sql = "select * from admin where AdminName='" + s1 + "' and AdminPwd='" + s2 + "'";
        System.out.println(sql);
        ResultSet rs = stmt.executeQuery(sql);
        if (!rs.next()) {
            OK = false;
        }
        return OK;
    }

    public List<AdminBean> getList() {
        Connection conn = DBConn.getConn();
        String sql = "select * from Admin limit \"+(page-1)*10+\",10";
        List<AdminBean> list = new ArrayList<AdminBean>();
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                AdminBean AdminBean = new AdminBean();
                AdminBean.setAdminID(rs.getInt("AdminID"));
                AdminBean.setAdminName(rs.getString("AdminName"));
                AdminBean.setAdminPwd(rs.getString("AdminPwd"));
                AdminBean.setAdminType(rs.getInt("AdminType"));
                AdminBean.setLastLoginTime(rs.getString("AdminName"));
                list.add(AdminBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int add(AdminBean Admin) {
        Connection conn = DBConn.getConn();
        String sql = "insert into Admin values(null,?,?,?,?)";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, Admin.getAdminPwd());
            pmst.setString(2, Admin.getAdminName());
            pmst.setString(3, Time.getDate());
            pmst.setString(4, Admin.getAdminName());
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//添加失败返回0
    }

    public int update(AdminBean Admin) {
        Connection conn = DBConn.getConn();
        String sql = "update Admin set  AdminPwd=?,AdminName=?,LastLoginTime=? where AdminID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, Admin.getAdminPwd());
            pmst.setString(2, Admin.getAdminName());
            pmst.setString(3,Time.getDate());
            pmst.setInt(4, Admin.getAdminID());
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//添加失败返回0
    }

    public int delete(int AdminID) {
        Connection conn = DBConn.getConn();
        try {
            String sql = "delete from Admin where AdminID=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1, AdminID);
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int count() {
        Connection conn = DBConn.getConn();
        try {
            String sql = "select count(*) from news";
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);//cout查询返回第一行数据 第一行第一列就是总条数
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
